//
//  DDShare.m
//  dingdang
//
//  Created by lichq on 7/24/15.
//  Copyright (c) 2015 阮 沧晖. All rights reserved.
//

#import "DDShare.h"
#import "PlaceholderTextView.h"
#import "UICommonFunc.h"
#import "WeiboSDK.h"
#import "AppDelegate.h"
#import <TencentOpenAPI/QQApiInterfaceObject.h>
#import <TencentOpenAPI/QQApiInterface.h>
#import "WXApiObject.h"

#define kRedirectURI    @"https://api.weibo.com/oauth2/default.html"

@interface DDShare ()

@property(weak, nonatomic) IBOutlet UITextField *tf_phoneNum;
@property(weak, nonatomic) IBOutlet PlaceholderTextView *textView_body;

@end

@implementation DDShare

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self createUI];
    
    self.tf_phoneNum.layer.cornerRadius = 2;
    self.tf_phoneNum.layer.masksToBounds = YES;
    self.tf_phoneNum.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.tf_phoneNum.layer.borderWidth = .5;
    
    self.textView_body.layer.cornerRadius = 10;
    self.textView_body.layer.masksToBounds = YES;
    self.textView_body.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.textView_body.layer.borderWidth = .5;
    self.textView_body.placeholderColor = [UIColor lightGrayColor];
//    self.textView_body.placeholder = @"请输入问题的文字描述";
}

- (void)createUI
{
    //是否安装了qq
    _qqLoginBtn.hidden = ![QQApiInterface isQQInstalled];
    //是否安装了微博
    _weiboLoginBtn.hidden = ![WeiboSDK isWeiboAppInstalled];
    //是否安装了微信
    _weixinLoginBtn.hidden = ![WXApi isWXAppInstalled];
    //微信登录还未申请通过
    _weixinLoginBtn.hidden = YES;
    
    self.view.backgroundColor = RGB(245, 245, 245);
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBarHidden = YES;
    //自定义TAB BAR UI
    [self addNavigatorTitle:NSLocalizedString(@"分享好友", nil) parent:self.view];
    self.barViewControl.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_Background"]];
    [self AddLeftBtnAction:nil normal:@"top_返回" selected:@"top_返回" action:^{
        //[self dismissViewControllerAnimated:YES completion:nil];
        [self.navigationController popViewControllerAnimated:YES];
    }];
}


//添加通讯录联系人
- (IBAction)addPhone:(id)sender{
    [UICommonFunc showAlert:@"添加"];
}

//发送
- (IBAction)post:(id)sender{
    //默认采用短信形式进行分享，分享出去的包括二维码+分享内容+应用下载地址
    NSString *string = [NSString stringWithFormat:@"发送的内容为:%@", self.textView_body.text];
    [UICommonFunc showAlert:string];
}

//QQ分享
- (IBAction)shareQQ:(id)sender{
    //        [UICommonFunc showAlert:@"QQ分享"];
    QQApiTextObject* txtObj = [QQApiTextObject objectWithText:self.textView_body.text];
    SendMessageToQQReq* req = [SendMessageToQQReq reqWithContent:txtObj];
    
    QQApiSendResultCode sent = [QQApiInterface sendReq:req];
    [self handleSendResult:sent];
}

- (void)handleSendResult:(QQApiSendResultCode)sendResult
{
    switch (sendResult)
    {
        case EQQAPIAPPNOTREGISTED:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"App未注册" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];
            
            break;
        }
        case EQQAPIMESSAGECONTENTINVALID:
        case EQQAPIMESSAGECONTENTNULL:
        case EQQAPIMESSAGETYPEINVALID:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"发送参数错误" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];
            
            break;
        }
        case EQQAPIQQNOTINSTALLED:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"未安装手Q" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];
            
            break;
        }
        case EQQAPIQQNOTSUPPORTAPI:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"API接口不支持" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];
            
            break;
        }
        case EQQAPISENDFAILD:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"发送失败" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];
            
            break;
        }
        default:
        {
            break;
        }
    }
}

//微博分享
- (IBAction)shareWeibo:(id)sender{
//    [UICommonFunc showAlert:@"微博分享"];
    
    WBAuthorizeRequest *authRequest = [WBAuthorizeRequest request];
    authRequest.redirectURI = kRedirectURI;
    authRequest.scope = @"all";
    
    WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequest requestWithMessage:[self messageToShare] authInfo:authRequest access_token:WEIXIN_AppID];
    request.userInfo = @{@"ShareMessageFrom": @"SendMessageToWeiboViewController",
                         @"Other_Info_1": [NSNumber numberWithInt:123],
                         @"Other_Info_2": @[@"obj1", @"obj2"],
                         @"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}};
    //    request.shouldOpenWeiboAppInstallPageIfNotInstalled = NO;
    [WeiboSDK sendRequest:request];
}

#pragma mark -
#pragma Internal Method

- (WBMessageObject *)messageToShare
{
    WBMessageObject *message = [WBMessageObject message];
    message.text = NSLocalizedString(self.textView_body.text, nil);
    return message;
}


//微信分享
- (IBAction)shareWeixin:(id)sender{
//    [UICommonFunc showAlert:@"微信分享"];
    SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
    req.text = self.textView_body.text;
    req.bText = YES;
    req.scene = WXSceneTimeline;
    
    [WXApi sendReq:req];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"\n"]) {
        [self.textView_body resignFirstResponder];
    }
    return YES;
}

- (void)textViewDidEndEditing:(UITextView *)textView{
    [self.textView_body resignFirstResponder];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.textView_body resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
